﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SviluppoDistinta.Library;

namespace SviluppoDistinta.WinApp
{
    public partial class DistintaDiLavoro : Form
    {

        private Ordine ordine;
        private List<int> RV;

        //public void resize(int larg, int alt);
        //{
        //    this.Width = larg;
        //    this.Height = alt;
        //}

        public DistintaDiLavoro(Ordine ordine)
        {
            this.ordine = ordine;
            RV = ordine.RigheVuote;

            InitializeComponent();
        }

        public void GeneraDistinta()
        {
            int larg = 542;
            int alt = 140;

            DataGridViewCellStyle styleY = new DataGridViewCellStyle();
            styleY.ForeColor = Color.Blue;
            styleY.BackColor = Color.Gainsboro;

            for (int jcol = 1; jcol < (ordine.MaxX + 1); jcol++)
            {
                DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();


                int index_X = grdDistintaLavoro.Columns.Add(col);
                grdDistintaLavoro.Columns[index_X].Width = 36;
                larg += 36;
                grdDistintaLavoro.Columns[index_X].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                grdDistintaLavoro.Columns[index_X].HeaderText = jcol.ToString();
                grdDistintaLavoro.Columns[index_X].HeaderCell.Style = styleY;

            }
            for (int riga = 1; riga < ordine.MaxY; riga++)
            {
                int index_Y = grdDistintaLavoro.Rows.Add();
                grdDistintaLavoro.Rows[index_Y].Height = 25;
                alt += 25;
            }

            txtArticolo.Text = ordine.Articolo.Nome;
            txtBolla.Text = ordine.Bolla;
            txtTotCapi.Text = ordine.NumeroTotCapi.ToString();
            txtTotPacchi.Text = (ordine.Pacchi.Count).ToString();


            foreach (Pacco pc in ordine.Pacchi)
            {
                FontFamily fam = new FontFamily("Microsoft Sans Serif");
                Font newFont = new Font(fam, 8.25f, FontStyle.Bold);
                Font italic = new Font(fam, 8.25f, FontStyle.Bold ^ FontStyle.Italic);



                grdDistintaLavoro.Rows[(pc.YRif - 1)].Cells[pc.XRif + 9].Value = pc.NumeroPezzi.ToString();
                //if (pc.NumeroPezzi < 25)
                //{
                //    grdDistintaLavoro.Rows[(pc.YRif - 1)].Cells[pc.XRif + 9].Style.Font = italic;
                //    grdDistintaLavoro.Rows[(pc.YRif - 1)].Cells[pc.XRif + 9].Style.ForeColor = Color.Red;
                //}
                //else
                grdDistintaLavoro.Rows[(pc.YRif - 1)].Cells[pc.XRif + 9].Style.Font = newFont;

                int numCapi = pc.Articolo.Ordine.GetNumeroCapi(pc.Colore, pc.Taglia);

                grdDistintaLavoro.Rows[(pc.YRif - 1)].Cells[0].Value = pc.Colore.ToString();
                grdDistintaLavoro.Rows[(pc.YRif - 1)].Cells[1].Value = numCapi * pc.Parte.NumeroNecessario / pc.Parte.PerNumeroCapi;
                grdDistintaLavoro.Rows[(pc.YRif - 1)].Cells[2].Value = "-";
                grdDistintaLavoro.Rows[(pc.YRif - 1)].Cells[3].Value = pc.Taglia.RS15;
                grdDistintaLavoro.Rows[(pc.YRif - 1)].Cells[4].Value = pc.Parte.RS16;
                grdDistintaLavoro.Rows[(pc.YRif - 1)].Cells[5].Value = pc.Parte.ToString();
                grdDistintaLavoro.Rows[(pc.YRif - 1)].Cells[6].Value = pc.Taglia.ToString();
                grdDistintaLavoro.Rows[(pc.YRif - 1)].Cells[7].Value = numCapi * pc.Parte.NumeroNecessario / pc.Parte.PerNumeroCapi;
                grdDistintaLavoro.Rows[(pc.YRif - 1)].Cells[9].Value = pc.YRif.ToString();

                grdDistintaLavoro.Rows[(pc.YRif - 1)].Cells[9].Style = styleY;

                if (pc.Primo)
                {
                    grdDistintaLavoro.Rows[(pc.YRif - 1)].Cells[8].Value = numCapi;
                }

            }
            DataGridViewCellStyle StileRigaVuota = new DataGridViewCellStyle();
            StileRigaVuota.BackColor = Color.Gainsboro;

            int j = 0;
            foreach (int i in RV)
            {
                if ((j + 1) < (RV.Count))
                {
                    grdDistintaLavoro.Rows.Insert((i + j), new DataGridViewRow());
                    grdDistintaLavoro.Rows[i + j].Height = 10;
                    for (int c = 0; c < grdDistintaLavoro.ColumnCount; c++)
                    {
                        grdDistintaLavoro.Rows[(i + j)].Cells[c].Style = StileRigaVuota;

                    }
                    alt += 10;
                    j++;
                }
            }
            int x_schermo = Screen.PrimaryScreen.Bounds.Width;
            int y_schermo = Screen.PrimaryScreen.Bounds.Height;

            if (larg > (x_schermo - 20)) larg = x_schermo - 20;
            if (larg < 745) larg = 745;
            this.Width = larg;
            if (alt > (y_schermo - 35)) alt = y_schermo - 35;
            this.Height = alt;
            this.Left = 0;
            this.Top = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            Reports.DistintaLavoro rptDist = new Reports.DistintaLavoro();
            rptDist.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperA4;
            rptDist.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Landscape;
            
            rptDist.PrintOptions.ApplyPageMargins(new CrystalDecisions.Shared.PageMargins(0, 0, 0, 0));
            rptDist.SetDataSource(ordine.Pacchi);

            rptDist.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, @"E:\Users\Matte\Desktop\rpt.pdf");


            //ReportViewer rv = new ReportViewer();
            //rv.crystalReportViewer1.ReportSource = rptDist;
            //rv.Show();
        }
    }
}
